package com.goal98.team.controllers

import grails.converters.JSON
import com.goal98.team.domain.Page
import com.goal98.team.Constants

class TourController {

    def index = {

        def id = params.id ? params.id : ''
        def ref = Constants.REF_TOUR_PAGE + id
        def next = params.id?Long.parseLong(params.id)+1:1
        def tourPage = Page.findByReference(ref, [cache:true])
        def nextRef = Constants.REF_TOUR_PAGE + next
        def nextPage = Page.findByReference(nextRef, [cache:true])
        if(!nextPage)
            next = null
        def steps = Page.findAllByReferenceLike(Constants.REF_TOUR_PAGE + "%", [sort:"reference", order:"asc"], [cache:true])
        [tourPage: tourPage, next:next, steps:steps]

    }

    def save = {
        def text = params.text
        def id = params.id
        Page tourPage = Page.get(id)
        tourPage.text = text
        tourPage.save()
        render(view:'index', model:[tourPage:tourPage])
    }

    def saveAjax = {
        def text = params.text
        def id = params.id
        Page tourPage = Page.get(id)
        tourPage.text = text
        tourPage.save()
        render Page.get(id) as JSON

    }
}
